package com.wsp.service.common;

import com.wsp.service.util.LockKeepLiveUtil;
import com.wsp.service.util.RedisUtil;
import com.wsp.service.util.ScheduledExecutorServiceUtil;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;

import java.time.Duration;

/**
 * Package: com.wsp.service.common
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
public final class OpenLockKeepLive implements SpringApplicationRunListener {
    private LockKeepLiveUtil lockKeepLiveUtil = new LockKeepLiveUtil();
    @Override
    public void ready(ConfigurableApplicationContext context, Duration timeTaken) {
        RedisUtil redisUtil;
        try {
            redisUtil = context.getBean(RedisUtil.class);
        } catch (NoSuchBeanDefinitionException e) {
            context.getBeanFactory().createBean(RedisUtil.class);
            redisUtil = context.getBean(RedisUtil.class);
        }
        lockKeepLiveUtil.setRedisUtil(redisUtil);
        ScheduledExecutorServiceUtil.addTask(lockKeepLiveUtil,0,Const.expireTime/4);
    }
}
